package com.uyayo.framework.pagehelper;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 分页参数类
 * uyayo-framework-pagehelper
 * Created by hfj on 2017/2/2.
 */
@ApiModel(value = "分页参数类")
public class Pageable<T> implements Serializable {

    private static final long serialVersionUID = 3824124414176766038L;

    @ApiModelProperty(value = "当前页",example = "1",required = true)
    private int currentPage = 1;

    @ApiModelProperty(value = "每页行数",example = "10",required = true)
    private int pageSize = 10;

    @ApiModelProperty("查询条件")
    private T condition;

    @ApiModelProperty("排序集合")
    private List<SortOrder> sortOrders;

    public Pageable() {
    }

    public void setSortOrders(List<SortOrder> sortOrders) {
        this.sortOrders = sortOrders;
    }

    public Pageable(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    public Pageable(int currentPage, int pageSize, List<SortOrder> sortOrders) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.sortOrders = sortOrders;
    }

    public Pageable(int currentPage, int pageSize, List<SortOrder> sortOrders,T condition) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.sortOrders = sortOrders;
        this.condition = condition;
    }

    public List<SortOrder> getSortOrders() {
        return sortOrders;
    }

    public Pageable next() {
        return new Pageable(getCurrentPage() + 1, getPageSize(), getSortOrders(),getCondition());
    }

    public Pageable previousOrFirst() {
        return hasPrevious() ? previous() : first();
    }

    private Pageable previous() {
        return getPageSize() == 1 ? this : new Pageable(getPageSize() - 1, getPageSize(), getSortOrders(),getCondition());
    }

    public Pageable first() {
        return new Pageable(1, getPageSize(), getSortOrders());
    }

    public boolean hasPrevious() {
        return currentPage > 1;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public T getCondition() {
        return condition;
    }

    public void setCondition(T condition) {
        this.condition = condition;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Pageable)) return false;
        Pageable that = (Pageable) o;
        return getCurrentPage() == that.getCurrentPage() &&
                getPageSize() == that.getPageSize() &&
                Objects.equals(getSortOrders(), that.getSortOrders());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCurrentPage(), getPageSize(), getSortOrders());
    }

    @Override
    public String toString() {
        return "Pageable{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", condition=" + condition +
                ", sortOrders=" + sortOrders +
                '}';
    }
}
